package com.idak.jms.sender;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.idak.jms.model.Customer;

@Service("customerSender")
public class CustomerSenderImpl implements CustomerSender{

	@Autowired
	private JmsTemplate jmsTemplate;
	
	public void createCustomer(final Customer  customer){
		MessageCreator messageCreator = new CustomerMessage(customer);
		jmsTemplate.send(messageCreator);
	}
	
	class CustomerMessage implements MessageCreator{
		
		private Customer customer;
		public CustomerMessage(Customer customer) {
			this.customer = customer;
		}

		@Override
		public Message createMessage(Session session) throws JMSException {
			Message message = session.createMessage();
			message.setIntProperty("customerId", customer.getCustomerId());
			message.setStringProperty("lastName", customer.getLastName());
			message.setStringProperty("firstName", customer.getFirstName());
			message.setStringProperty("email", customer.getEmail());
			message.setStringProperty("phone", customer.getPhoneNumber());
			return message;
		}
		
	}
}

